﻿package javase.socket.server;
/**
 * @author wangmj
 *可以通过这个方法将服务器收到的信息广播给大家。
 * 首先 java MulticastServer2
 * 然后 java MulticastClient
 * 服务器最终会把从文件中读出的信息广播给大家。
 * MulticastServer2.java
 */
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;  //未知错误类
import java.util.Date;

public class MulticastServer 
{
	public static void main(String[] args) throws java.io.IOException  //主函数抛出异常
	{
		try
		{
			new MulticastServerThread().start();
		}
		catch(IOException IOE)
		{
		}
	}
}

class MulticastServerThread extends QuoteServerThread2
{
	private long FIVE_SECONDS = 5000;
	//public MulticastServerThread
	public MulticastServerThread() throws IOException
	{
		super("MulticastServerThread");
	}
	boolean moreQuotes = true;
	String dString = null;
	public void run()
	{
		while(moreQuotes)
		{
			try
			{
				byte[] buf = new byte[256];
				//construct quote 引用, 引证, 提供, 提出, 报(价)
				Date date = null;
				date = new Date();
				if(dString == null)
				{
					dString = new String(date.toString());
				}
				else 
				{
					dString = getNextQuote();
				}
				buf = dString.getBytes();
				//send it
				InetAddress group = null;
				try 
				{
					group = InetAddress.getByName("230.0.0.1"); //获得IP地址
				} 
				catch(UnknownHostException e) 
				{
					e.printStackTrace();
				}
				DatagramPacket packet = new DatagramPacket(buf,buf.length,group,4446);
				socket.send(packet);  //发送封包
				//sleep for a while
				try
				{
					sleep((long)(Math.random()*FIVE_SECONDS));  //线程睡眠
				}
				catch(InterruptedException e)
				{
					e.getMessage();
				}
			}
			catch(IOException e)
			{
				e.printStackTrace();
				moreQuotes = false;
			}
		}
		socket.close();
	}
}

class QuoteServerThread2 extends Thread
{
	protected DatagramSocket socket = null;
	protected BufferedReader in = null;
	protected boolean moreQuotes = true;
	public QuoteServerThread2() throws IOException
	{
		this("QuoteServerThread");
	}
	public QuoteServerThread2(String name) throws IOException
	{
		super(name);
		socket = new DatagramSocket(4446);  //设置端口
		try
		{
			in = new BufferedReader(new FileReader("one-liners.txt"));
		}
		catch(FileNotFoundException e)
		{
			System.err.println("Could not open quote file.Serving time instead.");
		}
	}
	public void run()
	{
		while(moreQuotes)
		{
			try
			{
				byte[] buf = new byte[256];
				//receive reques
				DatagramPacket packet = new DatagramPacket(buf,buf.length);
				socket.receive(packet);  //接收封包
				//figure out response
				String dString = null;
				if(in == null)
				{
					dString = new Date().toString();
				}
				else
				{
					dString = getNextQuote();
				}
				buf = dString.getBytes();
				//send the response to the client at "address" and "potr"
				InetAddress address = packet.getAddress();
				int port = packet.getPort();
				packet = new DatagramPacket(buf,buf.length,address,port);
				socket.send(packet);
			}
			catch(IOException e)
			{
				e.printStackTrace();
				moreQuotes = false;
			}
		}
		socket.close();
	}
	protected String getNextQuote()
	{
		String value = null;
		try
		{
			value=in.readLine();
			if(value==null)
			{
				in.close();
				moreQuotes = false;
				value="No more quotes.Goodbye.";
			}
		}
		catch(IOException e)
		{
			value = "IOException occurred in server";
		}
		finally
		{
			System.out.println("getNextQuote "+value);
		}
		return value;
	}
}